
package com.yimixian.app.adapter;

import com.squareup.picasso.Picasso;
import com.umeng.analytics.MobclickAgent;
import com.yimixian.app.EventBusDomain.CommentReportMessage;
import com.yimixian.app.R;
import com.yimixian.app.SystemFramework;
import com.yimixian.app.common.Constants;
import com.yimixian.app.common.UMengConstants;
import com.yimixian.app.data.DataManager;
import com.yimixian.app.goods.GoodDetailActivity;
import com.yimixian.app.goods.GoodDetailTopFragment;
import com.yimixian.app.model.Comment;
import com.yimixian.app.model.ImageInfo;
import com.yimixian.app.ui.MessageDialogView;
import com.yimixian.app.ui.wheelview.CommentDialog;
import com.yimixian.app.util.UiUtils;
import com.ymx.sdk.util.DeviceUtils;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by songzy on 2015/10/23.
 */
public class CommentAdapter extends BaseAdapter {
    public List mCommentList;
    private Context mContext;
    private Activity mActivity;
    public TextView mImageTxtView;
    public TextView mCommentView;
    public TextView mRedPointView;
    private String mCurrentTab = GoodDetailTopFragment.IMAGE_TXT_TAB;
    private boolean mCanComment = false;
    private boolean mCommentShow = false;
    private String mBtnName = "";
    private View dialog;

    public CommentAdapter(Context context,Activity activity, List commentList) {
        mContext = context;
        mActivity = activity;
        mCommentList = new ArrayList();
        mCommentList.addAll(commentList);
    }

    @Override
    public int getCount() {
        return mCommentList == null ? 1 : mCommentList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mCommentList == null ? null : mCommentList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        if (position == 0) {
            return getSelectView();
        }
        position = position - 1;
        if (mCommentList.get(position) instanceof ImageInfo) {
            return getImageTxtView(position);
        } else {
            return getCommentView(position, convertView);
        }
    }

    public void setCurrentTab(String currentTab) {
        this.mCurrentTab = currentTab;
    }

    /**
     * 获得选择view
     */
    private View getSelectView() {
        View view = View.inflate(mContext, R.layout.fragment_good_detail_selection, null);
        TextView commentFruit = (TextView) view.findViewById(R.id.comment_fruit);
        if(!TextUtils.isEmpty(mBtnName)) {
            commentFruit.setText(mBtnName);
        }
        mImageTxtView = (TextView) view.findViewById(R.id.tv_image_txt);
        mCommentView = (TextView) view.findViewById(R.id.tv_comment);
        mRedPointView = (TextView) view.findViewById(R.id.order_point);
        if(mCanComment) {
            if(GoodDetailTopFragment.COMMENT_TAB.equals(mCurrentTab)) {
                commentFruit.setVisibility(View.VISIBLE);
            } else {
                commentFruit.setVisibility(View.GONE);
            }
            mRedPointView.setVisibility(View.VISIBLE);
        } else {
            commentFruit.setVisibility(View.GONE);
            mRedPointView.setVisibility(View.GONE);
        }
        if(mCommentShow) {
            mCommentView.setVisibility(View.VISIBLE);
        } else {
            mCommentView.setVisibility(View.GONE);
        }
//        if(mCommentList != null && mCommentList.size() > 0){
//            mImageTxtView.setVisibility(View.VISIBLE);
//        }else{
//            mImageTxtView.setVisibility(View.GONE);
//        }
        mImageTxtView.setVisibility(View.VISIBLE);
        if (GoodDetailTopFragment.IMAGE_TXT_TAB.equals(mCurrentTab)) {
            mImageTxtView.setTextColor(mContext.getResources().getColor(R.color.ymx_orange));
            mImageTxtView.setBackgroundColor(mContext.getResources().getColor(R.color.white));
            mCommentView.setBackgroundColor(mContext.getResources().getColor(R.color.detail_comment_tab));
            mCommentView.setTextColor(mContext.getResources().getColor(R.color.home_black_color));
        } else {
            mCommentView.setTextColor(mContext.getResources().getColor(R.color.ymx_orange));
            mCommentView.setBackgroundColor(mContext.getResources().getColor(R.color.white));
            mImageTxtView.setBackgroundColor(mContext.getResources().getColor(R.color.detail_comment_tab));
            mImageTxtView.setTextColor(mContext.getResources().getColor(R.color.home_black_color));
        }
        commentFruit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mContext != null) {
                    MobclickAgent.onEvent(SystemFramework.getInstance().getGlobalContext(),
                        UMengConstants.NEW_GOODDETAIL_ADDCOMMENT);
                    CommentDialog dialog = new CommentDialog(mContext);
                    dialog.showDialog();
                }
            }
        });
        mImageTxtView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EventBus.getDefault().post(Constants.EVENT_BUS_GOODS_DETAIL_IMAGE);
            }
        });
        mCommentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MobclickAgent.onEvent(SystemFramework.getInstance().getGlobalContext(),
                    UMengConstants.NEW_GOODDETAIL_COMMENTBTN);
                EventBus.getDefault().post(Constants.EVENT_BUS_GOODS_DETAIL_COMMENT);
            }
        });
        return view;
    }

    /**
     * 获取评论item
     * 
     * @param position
     * @param convertView
     * @return
     */
    @NonNull
    private View getCommentView(int position, View convertView) {
        if (convertView instanceof FrameLayout) {
            convertView = null;
        }
        ConvertViewHolder holder;
        if (convertView == null) {
            holder = new ConvertViewHolder();
            convertView = View.inflate(mContext, R.layout.goodsdetail_comment_item, null);
            holder.mNameTextView = (TextView) convertView.findViewById(R.id.text_name);
            holder.mContentTextView = (TextView) convertView.findViewById(R.id.text_content);
            holder.mRatingBar1 = (RatingBar) convertView.findViewById(R.id.ratingbar_1);
            holder.mRatingBar2 = (RatingBar) convertView.findViewById(R.id.ratingbar_2);
            holder.mFreshnessView = (TextView) convertView.findViewById(R.id.freshness);
            holder.mTasteView = (TextView) convertView.findViewById(R.id.taste);
            holder.mCommentReport = (TextView) convertView.findViewById(R.id.comment_report);
            convertView.setTag(holder);
        } else {
            holder = (ConvertViewHolder) convertView.getTag();
        }
        final Comment comment = (Comment) mCommentList.get(position);
        if (comment != null && holder != null) {
            holder.mNameTextView.setText(comment.reviewerName);
            holder.mContentTextView.setText(comment.content);
            holder.mRatingBar1.setRating(comment.taste);
//            SharedPreferencesHelper.saveRatingBarText1(comment.freshnessDesc);
//            SharedPreferencesHelper.saveRatingBarText2(comment.tasteDesc);
            holder.mFreshnessView.setText(comment.freshnessDesc + ":");
            holder.mTasteView.setText(comment.tasteDesc + ":");
            holder.mRatingBar2.setRating(comment.freshness);
            holder.mCommentReport.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mActivity != null && mActivity instanceof GoodDetailActivity) {
                        dialog = UiUtils.showOKAndCancelDialog(mActivity, "确认要举报该评论吗？", "确认", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                UiUtils.hideDialog(dialog);
                                CommentReportMessage message = new CommentReportMessage();
                                if (comment != null) {
                                    message.commentId = comment.id;
                                }
                                EventBus.getDefault().post(message);
                            }
                        }, "取消", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                UiUtils.hideDialog(dialog);
                            }
                        });
//                        final MessageDialogView messageDialogView = new MessageDialogView(
//                            mActivity);
//                        messageDialogView.bindConfirm("提示", "确认要举报该评论吗？",
//                            new View.OnClickListener() {
//                                @Override
//                                public void onClick(View v) {
//                                    CommentReportMessage message = new CommentReportMessage();
//                                    if (comment != null) {
//                                        message.commentId = comment.id;
//                                    }
//                                    EventBus.getDefault().post(message);
//                                    if (((GoodDetailActivity) mActivity).mDialogContainer != null) {
//                                        ((GoodDetailActivity) mActivity).mDialogContainer.removeView(messageDialogView);
//                                    }
//                                }
//                            }, new View.OnClickListener() {
//                                @Override
//                                public void onClick(View view) {
//                                    if (((GoodDetailActivity) mActivity).mDialogContainer != null) {
//                                        ((GoodDetailActivity) mActivity).mDialogContainer.removeView(messageDialogView);
//                                    }
//                                }
//                            });
//                        if (((GoodDetailActivity) mActivity).mDialogContainer != null) {
//                            ((GoodDetailActivity) mActivity).mDialogContainer.addView(messageDialogView);
//                        }
                    }
                }
            });
        }
        return convertView;
    }

    /**
     * 获取图文详情页面
     * 
     * @param position
     * @return
     */
    private View getImageTxtView(int position) {
        //更新图文详情
        View view = View.inflate(mContext, R.layout.fragment_good_detail_pic_txt, null);
        TextView mTvGoodDesc = (TextView) view.findViewById(R.id.tv_good_desc);
        ImageView mIvIntroImg = (ImageView)view.findViewById(R.id.image_view);

        ImageInfo info = (ImageInfo)mCommentList.get(position);
        if(info != null) {
            if (position == 0) {
                if (TextUtils.isEmpty(info.title)) {
                    mTvGoodDesc.setVisibility(View.GONE);
                } else {
                    mTvGoodDesc.setText("功效：" + info.title);
                    mTvGoodDesc.setVisibility(View.VISIBLE);
                }
            } else {
                mTvGoodDesc.setVisibility(View.GONE);
            }

            if (TextUtils.isEmpty(info.image)) {
                mIvIntroImg.setVisibility(View.GONE);
            } else {
                if(mActivity != null) {
                    int screenWidth = DeviceUtils.getScreenWidth(mActivity);
                    mIvIntroImg.setScaleType(ImageView.ScaleType.FIT_XY);
                    int height = (int) (screenWidth / info.image_ratio);
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT, height);
                    mIvIntroImg.setLayoutParams(params);
                    Picasso.with(mActivity).load(DataManager.getInstance().getImageUrl(info.image, screenWidth))
                        .placeholder(R.drawable.placeholder)
                        .into(mIvIntroImg);
                    mIvIntroImg.setVisibility(View.VISIBLE);
                }
            }
        }
        return view;
    }

    private class ConvertViewHolder {
        private TextView mNameTextView;
        private TextView mContentTextView;
        private RatingBar mRatingBar1;
        private RatingBar mRatingBar2;
        private TextView mTasteView;
        private TextView mFreshnessView;
        private TextView mCommentReport;
    }

    public void setCanComment(boolean canComment) {
        this.mCanComment = canComment;
    }

    public void setCommentShow(boolean commentShow) {
        mCommentShow = commentShow;
    }

    public void setBtnName(String btnName) {
        mBtnName = btnName;
    }
}
